<html><head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
   <title>14.&nbsp;Marshalling XML using O/X Mappers</title><link rel="stylesheet" href="css/stylesheet.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.74.0"><link rel="home" href="index.html" title="Reference Documentation"><link rel="up" href="spring-data-tier.html" title="Part&nbsp;IV.&nbsp;Data Access"><link rel="prev" href="orm.html" title="13.&nbsp;Object Relational Mapping (ORM) Data Access"><link rel="next" href="spring-web.html" title="Part&nbsp;V.&nbsp;The Web"><!--Begin Google Analytics code--><script type="text/javascript">
			var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
			document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
		</script><script type="text/javascript">
			var pageTracker = _gat._getTracker("UA-2728886-3");
			pageTracker._setDomainName("none");
			pageTracker._setAllowLinker(true);
			pageTracker._trackPageview();
		</script><!--End Google Analytics code--></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">14.&nbsp;Marshalling XML using O/X Mappers</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="orm.html">Prev</a>&nbsp;</td><th width="60%" align="center">Part&nbsp;IV.&nbsp;Data Access</th><td width="20%" align="right">&nbsp;<a accesskey="n" href="spring-web.html">Next</a></td></tr></table><hr></div><div class="chapter" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="oxm"></a>14.&nbsp;Marshalling XML using O/X Mappers</h2></div></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="oxm-introduction"></a>14.1&nbsp;Introduction</h2></div></div></div><p>
            In this chapter, we will describe Spring's Object/XML Mapping support. Object/XML Mapping, or O/X mapping
            for short, is the act of converting an XML document to and from an object. This conversion process is also
            known as XML Marshalling, or XML Serialization. This chapter uses these terms interchangeably.
        </p><p>
            Within the field of O/X mapping, a <span class="emphasis"><em>marshaller</em></span> is responsible for serializing an 
            object (graph) to XML. In similar fashion, an <span class="emphasis"><em>unmarshaller</em></span> deserializes the XML to an
            object graph. This XML can take the form of a DOM document, an input or output stream, or a SAX handler.
        </p><p>Some of the benefits of using Spring for your O/X mapping needs are:</p><p><b>Ease of configuration.&nbsp;</b>
                Spring's bean factory makes it easy to configure marshallers, without needing to construct JAXB context,
                JiBX binding factories, etc. The marshallers can be configured as any other bean in your application
                context. Additionally, XML Schema-based configuration is available for a number of marshallers, making
                the configuration even simpler.
            </p><p><b>Consistent Interfaces.&nbsp;</b>
                Spring's O/X mapping operates through two global interfaces: the 
                <code class="interfacename">Marshaller</code> and <code class="interfacename">Unmarshaller</code> interface. 
                These abstractions allow you to switch O/X mapping 
                frameworks with relative ease, with little or no changes required on the classes that do the 
                marshalling. This approach has the additional benefit of making it possible to do XML marshalling with 
                a mix-and-match approach (e.g. some marshalling performed using JAXB, other using XMLBeans) in a 
                non-intrusive fashion, leveraging the strength of each technology.
            </p><p><b>Consistent Exception Hierarchy.&nbsp;</b>
                Spring provides a conversion from exceptions from the underlying O/X mapping tool to its own exception
                hierarchy with the <code class="classname">XmlMappingException</code> as the root exception. As can be expected,
                these runtime exceptions wrap the original exception so no information is lost.
            </p></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="oxm-marshaller-unmarshaller"></a>14.2&nbsp;Marshaller and Unmarshaller</h2></div></div></div><p>
            As stated in the introduction, a <span class="emphasis"><em>marshaller</em></span> serializes an object to XML, and an
            <span class="emphasis"><em>unmarshaller</em></span> deserializes XML stream to an object. In this section, we will describe
            the two Spring interfaces used for this purpose.
        </p><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="d0e26096"></a>14.2.1&nbsp;Marshaller</h3></div></div></div><p>
                Spring abstracts all marshalling operations behind the 
                <code class="interfacename">org.springframework.oxm.Marshaller</code> interface, the main methods of which 
                is listed below.
                </p><pre class="programlisting">
<span class="hl-keyword">public</span> <span class="hl-keyword">interface</span> Marshaller {

    <span class="hl-comment">/**
     * Marshals the object graph with the given root into the provided Result.
     */</span>
    <span class="hl-keyword">void</span> marshal(Object graph, Result result)
        <span class="hl-keyword">throws</span> XmlMappingException, IOException;
}</pre><p>
                The <code class="interfacename">Marshaller</code> interface has one main method, which marshals the given
                object to a given <code class="interfacename">javax.xml.transform.Result</code>. Result is a tagging 
                interface that basically represents an XML output abstraction: concrete implementations wrap various XML
                representations, as indicated in the table below.
                </p><div class="informaltable"><table style="border-collapse: collapse;border-top: 1.0pt solid ; border-bottom: 1.0pt solid ; border-left: 1.0pt solid ; border-right: 1.0pt solid ; "><colgroup><col><col></colgroup><thead><tr><th style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; "><code class="interfacename">Result</code> implementation</th><th style="border-bottom: 1.0pt solid ; ">Wraps XML representation</th></tr></thead><tbody><tr><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; "><code class="classname">DOMResult</code></td><td style="border-bottom: 1.0pt solid ; "><code class="interfacename">org.w3c.dom.Node</code></td></tr><tr><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; "><code class="classname">SAXResult</code></td><td style="border-bottom: 1.0pt solid ; "><code class="interfacename">org.xml.sax.ContentHandler</code></td></tr><tr><td style="border-right: 1.0pt solid ; "><code class="interfacename">StreamResult</code></td><td style="">
                                    <code class="classname">java.io.File</code>,
                                    <code class="classname">java.io.OutputStream</code>, or
                                    <code class="classname">java.io.Writer</code>
                                </td></tr></tbody></table></div><p>
                </p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.gif"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>
                        Although the <code class="methodname">marshal()</code> method accepts a plain object as its first 
                        parameter, most <code class="classname">Marshaller</code> implementations cannot handle arbitrary 
                        objects. Instead, an object class must be mapped in a mapping file, marked with an annotation,
                        registered with the marshaller, or have a common base class. Refer to the further sections
                        in this chapter to determine how your O/X technology of choice manages this.
                    </p></td></tr></table></div><p>
            </p></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="d0e26164"></a>14.2.2&nbsp;Unmarshaller</h3></div></div></div><p>
                Similar to the <code class="interfacename">Marshaller</code>, there is the 
                <code class="interfacename">org.springframework.oxm.Unmarshaller</code> interface.
                </p><pre class="programlisting">
<span class="hl-keyword">public</span> <span class="hl-keyword">interface</span> Unmarshaller {

    <span class="hl-comment">/**
     * Unmarshals the given provided Source into an object graph.
     */</span>
    Object unmarshal(Source source)
        <span class="hl-keyword">throws</span> XmlMappingException, IOException;
}</pre><p>
                This interface also has one method, which reads from the given 
                <code class="interfacename">javax.xml.transform.Source</code> (an XML input abstraction), and returns the
                object read. As with Result, Source is a tagging interface that has three concrete implementations. Each
                wraps a different XML representation, as indicated in the table below.
                </p><div class="informaltable"><table style="border-collapse: collapse;border-top: 1.0pt solid ; border-bottom: 1.0pt solid ; border-left: 1.0pt solid ; border-right: 1.0pt solid ; "><colgroup><col><col></colgroup><thead><tr><th style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; "><code class="interfacename">Source</code> implementation</th><th style="border-bottom: 1.0pt solid ; ">Wraps XML representation</th></tr></thead><tbody><tr><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; "><code class="classname">DOMSource</code></td><td style="border-bottom: 1.0pt solid ; "><code class="interfacename">org.w3c.dom.Node</code></td></tr><tr><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; "><code class="classname">SAXSource</code></td><td style="border-bottom: 1.0pt solid ; ">
                                    <code class="classname">org.xml.sax.InputSource</code>, and
                                    <code class="interfacename">org.xml.sax.XMLReader</code>
                                </td></tr><tr><td style="border-right: 1.0pt solid ; "><code class="classname">StreamSource</code></td><td style="">
                                    <code class="classname">java.io.File</code>,
                                    <code class="classname">java.io.InputStream</code>, or
                                    <code class="classname">java.io.Reader</code>
                                </td></tr></tbody></table></div><p>
            </p></div><p>
            Even though there are two separate marshalling interfaces (<code class="interfacename">Marshaller</code>
            and <code class="interfacename">Unmarshaller</code>), all implementations found in Spring-WS implement both in
            one class. This means that you can wire up one marshaller class and refer to it both as a marshaller and an 
            unmarshaller in your <code class="filename">applicationContext.xml</code>.
        </p><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="d0e26238"></a>14.2.3&nbsp;XmlMappingException</h3></div></div></div><p>
                Spring converts exceptions from the underlying O/X mapping tool to its own exception hierarchy with the
                <code class="classname">XmlMappingException</code> as the root exception. As can be expected, these runtime
                exceptions wrap the original exception so no information will be lost.
            </p><p>
                Additionally, the <code class="classname">MarshallingFailureException</code> and
                <code class="classname">UnmarshallingFailureException</code> provide a distinction between marshalling and
                unmarshalling operations, even though the underlying O/X mapping tool does not do so.
            </p><p>
                The O/X Mapping exception hierarchy is shown in the following figure:
                </p><div class="mediaobject" align="center"><img src="images/oxm-exceptions.png" align="middle"><div class="caption"><p>
                            O/X Mapping exception hierarchy
                        </p></div></div><p>
            </p></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="oxm-usage"></a>14.3&nbsp;Using Marshaller and Unmarshaller</h2></div></div></div><p>
            Spring's OXM can be used for a wide variety of situations. In the following example, we will use it to
            marshal the settings of a Spring-managed application as an XML file. We will use a simple JavaBean to
            represent the settings:
            </p><pre class="programlisting">
<span class="hl-keyword">public</span> <span class="hl-keyword">class</span> Settings {
    <span class="hl-keyword">private</span> <span class="hl-keyword">boolean</span> fooEnabled;

    <span class="hl-keyword">public</span> <span class="hl-keyword">boolean</span> isFooEnabled() {
        <span class="hl-keyword">return</span> fooEnabled;
    }

    <span class="hl-keyword">public</span> <span class="hl-keyword">void</span> setFooEnabled(<span class="hl-keyword">boolean</span> fooEnabled) {
        <span class="hl-keyword">this</span>.fooEnabled = fooEnabled;
    }
}</pre><p>
        </p><p>
            The application class uses this bean to store its settings. Besides a main method, the class has two
            methods: <code class="methodname">saveSettings()</code> saves the settings bean to a file named
            <code class="filename">settings.xml</code>, and <code class="methodname">loadSettings()</code> loads these settings again. A
            <code class="methodname">main()</code> method constructs a Spring application context, and calls these two methods.
            </p><pre class="programlisting">
<span class="hl-keyword">import</span> java.io.FileInputStream;
<span class="hl-keyword">import</span> java.io.FileOutputStream;
<span class="hl-keyword">import</span> java.io.IOException;
<span class="hl-keyword">import</span> javax.xml.transform.stream.StreamResult;
<span class="hl-keyword">import</span> javax.xml.transform.stream.StreamSource;

<span class="hl-keyword">import</span> org.springframework.context.ApplicationContext;
<span class="hl-keyword">import</span> org.springframework.context.support.ClassPathXmlApplicationContext;
<span class="hl-keyword">import</span> org.springframework.oxm.Marshaller;
<span class="hl-keyword">import</span> org.springframework.oxm.Unmarshaller;

<span class="hl-keyword">public</span> <span class="hl-keyword">class</span> Application {
    <span class="hl-keyword">private</span> <span class="hl-keyword">static</span> <span class="hl-keyword">final</span> String FILE_NAME = <span class="hl-string">"settings.xml"</span>;
    <span class="hl-keyword">private</span> Settings settings = <span class="hl-keyword">new</span> Settings();
    <span class="hl-keyword">private</span> Marshaller marshaller;
    <span class="hl-keyword">private</span> Unmarshaller unmarshaller;

    <span class="hl-keyword">public</span> <span class="hl-keyword">void</span> setMarshaller(Marshaller marshaller) {
        <span class="hl-keyword">this</span>.marshaller = marshaller;
    }

    <span class="hl-keyword">public</span> <span class="hl-keyword">void</span> setUnmarshaller(Unmarshaller unmarshaller) {
        <span class="hl-keyword">this</span>.unmarshaller = unmarshaller;
    }

    <span class="hl-keyword">public</span> <span class="hl-keyword">void</span> saveSettings() <span class="hl-keyword">throws</span> IOException {
        FileOutputStream os = null;
        <span class="hl-keyword">try</span> {
            os = <span class="hl-keyword">new</span> FileOutputStream(FILE_NAME);
            <span class="hl-keyword">this</span>.marshaller.marshal(settings, <span class="hl-keyword">new</span> StreamResult(os));
        } <span class="hl-keyword">finally</span> {
            <span class="hl-keyword">if</span> (os != null) {
                os.close();
            }
        }
    }

    <span class="hl-keyword">public</span> <span class="hl-keyword">void</span> loadSettings() <span class="hl-keyword">throws</span> IOException {
        FileInputStream is = null;
        <span class="hl-keyword">try</span> {
            is = <span class="hl-keyword">new</span> FileInputStream(FILE_NAME);
            <span class="hl-keyword">this</span>.settings = (Settings) <span class="hl-keyword">this</span>.unmarshaller.unmarshal(<span class="hl-keyword">new</span> StreamSource(is));
        } <span class="hl-keyword">finally</span> {
            <span class="hl-keyword">if</span> (is != null) {
                is.close();
            }
        }
    }

    <span class="hl-keyword">public</span> <span class="hl-keyword">static</span> <span class="hl-keyword">void</span> main(String[] args) <span class="hl-keyword">throws</span> IOException {
        ApplicationContext appContext =
            <span class="hl-keyword">new</span> ClassPathXmlApplicationContext(<span class="hl-string">"applicationContext.xml"</span>);
        Application application = (Application) appContext.getBean(<span class="hl-string">"application"</span>);
        application.saveSettings();
        application.loadSettings();
    }
}</pre><p>
            The <code class="classname">Application</code> requires both a <span class="property">marshaller</span>
            and <span class="property">unmarshaller</span> property to be set. We can do so using the following
            <code class="filename">applicationContext.xml</code>:
            </p><pre class="programlisting">
&lt;<span class="hl-tag">beans</span>&gt;
    &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"application"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"Application"</span>&gt;
        &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"marshaller"</span> <span class="hl-attribute">ref</span>=<span class="hl-value">"castorMarshaller"</span> /&gt;
        &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"unmarshaller"</span> <span class="hl-attribute">ref</span>=<span class="hl-value">"castorMarshaller"</span> /&gt;
    &lt;<span class="hl-tag">/bean</span>&gt;
    &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"castorMarshaller"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.oxm.castor.CastorMarshaller"</span>/&gt;
&lt;<span class="hl-tag">/beans</span>&gt;
</pre><p>
            This application context uses Castor, but we could have used any of the other marshaller instances described
            later in this chapter. Note that Castor does not require any further configuration by default, so the bean
            definition is rather simple. Also note that the <code class="classname">CastorMarshaller</code> implements both
            <code class="interfacename">Marshaller</code> and <code class="interfacename">Unmarshaller</code>, so we can refer
            to the <code class="varname">castorMarshaller</code> bean in both the <span class="property">marshaller</span> and
            <span class="property">unmarshaller</span> property of the application.
        </p><p>
            This sample application produces the following <code class="filename">settings.xml</code> file:
            </p><pre class="programlisting">
&lt;<span class="hl-tag">?xml version="1.0" encoding="UTF-8"?</span>&gt;
&lt;<span class="hl-tag">settings</span> <span class="hl-attribute">foo-enabled</span>=<span class="hl-value">"false"</span>/&gt;
</pre><p>
        </p></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="d0e26329"></a>14.4&nbsp;XML Schema-based Configuration</h2></div></div></div><p>
       		Marshallers could be configured more concisely using tags from the OXM namespace.
       		To make these tags available, the appropriate schema has to be referenced first in the preamble of the XML configuration file.
       		The emboldened text	in the below snippet references the OXM schema:
       	</p><pre class="programlisting">&lt;<span class="hl-tag">?xml version="1.0" encoding="UTF-8"?</span>&gt;
&lt;<span class="hl-tag">beans</span> <span class="hl-attribute">xmlns</span>=<span class="hl-value">"http://www.springframework.org/schema/beans"</span>
    <span class="hl-attribute">xmlns:xsi</span>=<span class="hl-value">"http://www.w3.org/2001/XMLSchema-instance"</span>
    <span class="hl-attribute">xmlns:oxm</span>=<span class="hl-value">"http://www.springframework.org/schema/oxm"</span>
    <span class="hl-attribute">xsi:schemaLocation</span>=<span class="hl-value">"http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/oxm
    http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd"</span>&gt;
</pre><p>
			Currently, the following tags are available:
			</p><div class="itemizedlist"><ul type="disc"><li><p><a class="link" href="oxm.html#oxm-jaxb2-xsd" title="14.5.1.1&nbsp;XML Schema-based Configuration"><code class="literal">jaxb2-marshaller</code></a></p></li><li><p><a class="link" href="oxm.html#oxm-xmlbeans-xsd" title="14.7.1.1&nbsp;XML Schema-based Configuration"><code class="literal">xmlbeans-marshaller</code></a></p></li><li><p><a class="link" href="oxm.html#oxm-jibx-xsd" title="14.8.1.1&nbsp;XML Schema-based Configuration"><code class="literal">jibx-marshaller</code></a></p></li></ul></div><p>
		</p><p>
			Each tag will be explained in its respective marshaller's section. As an example though, here is how
			the configuration of a JAXB2 marshaller might look like:
		</p><pre class="programlisting">&lt;<span class="hl-tag">oxm:jaxb2-marshaller</span> <span class="hl-attribute">id</span>=<span class="hl-value">"marshaller"</span> <span class="hl-attribute">contextPath</span>=<span class="hl-value">"org.springframework.ws.samples.airline.schema"</span>/&gt;</pre></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="oxm-jaxb"></a>14.5&nbsp;JAXB</h2></div></div></div><p>
            The JAXB binding compiler translates a W3C XML Schema into one or more Java classes, a 
            <code class="filename">jaxb.properties</code> file, and possibly some resource files. JAXB also offers a 
            way to generate a schema from annotated Java classes.
        </p><p>
            Spring supports the JAXB 2.0 API as XML marshalling strategies, following the
            <code class="interfacename">Marshaller</code> and <code class="interfacename">Unmarshaller</code>
            interfaces described in <a class="xref" href="oxm.html#oxm-marshaller-unmarshaller" title="14.2&nbsp;Marshaller and Unmarshaller">Section&nbsp;14.2, &#8220;Marshaller and Unmarshaller&#8221;</a>. The corresponding integration 
            classes reside in the <span class="package">org.springframework.oxm.jaxb</span> package.
        </p><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="oxm-jaxb2"></a>14.5.1&nbsp;Jaxb2Marshaller</h3></div></div></div><p>
                The <code class="classname">Jaxb2Marshaller</code> class implements both the Spring
                <code class="interfacename">Marshaller</code> and <code class="interfacename">Unmarshaller</code>interface. It
                requires a context path to operate, which you can set using the <span class="property">contextPath</span>
                property. The context path is a list of colon (:) separated Java package names that contain schema
                derived classes. It also offers a <span class="property">classesToBeBound</span> property, which allows you to set an array of 
                classes to be supported by the marshaller. Schema validation is performed by specifying one or more 
                schema resource to the bean, like so:
            </p><pre class="programlisting">
&lt;<span class="hl-tag">beans</span>&gt;

    &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"jaxb2Marshaller"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.oxm.jaxb.Jaxb2Marshaller"</span>&gt;
        &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"classesToBeBound"</span>&gt;
            &lt;<span class="hl-tag">list</span>&gt;
                &lt;<span class="hl-tag">value</span>&gt;org.springframework.oxm.jaxb.Flight&lt;<span class="hl-tag">/value</span>&gt;
                &lt;<span class="hl-tag">value</span>&gt;org.springframework.oxm.jaxb.Flights&lt;<span class="hl-tag">/value</span>&gt;                
            &lt;<span class="hl-tag">/list</span>&gt;
        &lt;<span class="hl-tag">/property</span>&gt;
        &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"schema"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"classpath:org/springframework/oxm/schema.xsd"</span>/&gt;
    &lt;<span class="hl-tag">/bean</span>&gt;
    ...

&lt;<span class="hl-tag">/beans</span>&gt;</pre><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="oxm-jaxb2-xsd"></a>14.5.1.1&nbsp;XML Schema-based Configuration</h4></div></div></div><p>
	        		The <code class="literal">jaxb2-marshaller</code> tag configures a <code class="classname">org.springframework.oxm.jaxb.Jaxb2Marshaller</code>.
	        		Here is an example:
	        	</p><pre class="programlisting">&lt;<span class="hl-tag">oxm:jaxb2-marshaller</span> <span class="hl-attribute">id</span>=<span class="hl-value">"marshaller"</span> <span class="hl-attribute">contextPath</span>=<span class="hl-value">"org.springframework.ws.samples.airline.schema"</span>/&gt;</pre><p>
	        		Alternatively, the list of classes to bind can be provided to the marshaller via the <code class="literal">class-to-be-bound</code> child tag:
	        	</p><pre class="programlisting">&lt;<span class="hl-tag">oxm:jaxb2-marshaller</span> <span class="hl-attribute">id</span>=<span class="hl-value">"marshaller"</span>&gt;
    &lt;<span class="hl-tag">oxm:class-to-be-bound</span> <span class="hl-attribute">name</span>=<span class="hl-value">"org.springframework.ws.samples.airline.schema.Airport"</span>/&gt;
    &lt;<span class="hl-tag">oxm:class-to-be-bound</span> <span class="hl-attribute">name</span>=<span class="hl-value">"org.springframework.ws.samples.airline.schema.Flight"</span>/&gt;
    ...
&lt;<span class="hl-tag">/oxm:jaxb2-marshaller</span>&gt;
	        	</pre><p>
	        		Available attributes are:
                    </p><div class="informaltable"><table style="border-collapse: collapse;border-top: 1.0pt solid ; border-bottom: 1.0pt solid ; border-left: 1.0pt solid ; border-right: 1.0pt solid ; "><colgroup><col><col><col></colgroup><thead><tr><th style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; ">Attribute</th><th style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; ">Description</th><th style="border-bottom: 1.0pt solid ; ">Required</th></tr></thead><tbody><tr><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; "><code class="literal">id</code></td><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; ">the id of the marshaller</td><td style="border-bottom: 1.0pt solid ; ">no</td></tr><tr><td style="border-right: 1.0pt solid ; "><code class="literal">contextPath</code></td><td style="border-right: 1.0pt solid ; ">the JAXB Context path</td><td style="">no</td></tr></tbody></table></div><p>
	        	</p></div></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="oxm-castor"></a>14.6&nbsp;Castor</h2></div></div></div><p>
            Castor XML mapping is an open source XML binding framework. It allows you to transform the data contained in
            a java object model into/from an XML document. By default, it does not require any further configuration,
            though a mapping file can be used to have more control over the behavior of Castor.
        </p><p>
            For more information on Castor, refer to the <a class="ulink" href="http://castor.org/xml-framework.html" target="_top">
            <em class="citetitle">Castor web site</em></a>. The Spring integration classes reside in the
            <span class="package">org.springframework.oxm.castor</span> package.
        </p><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="d0e26476"></a>14.6.1&nbsp;CastorMarshaller</h3></div></div></div><p>
                As with JAXB, the <code class="classname">CastorMarshaller</code> implements both the 
                <code class="interfacename">Marshaller</code> and <code class="interfacename">Unmarshaller</code> interface. 
                It can be wired up as follows:
            </p><pre class="programlisting">
&lt;<span class="hl-tag">beans</span>&gt;

    &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"castorMarshaller"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.oxm.castor.CastorMarshaller"</span> /&gt;
    ...

&lt;<span class="hl-tag">/beans</span>&gt;</pre></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="d0e26492"></a>14.6.2&nbsp;Mapping</h3></div></div></div><p>
                Although it is possible to rely on Castor's default marshalling behavior, it might be necessary to have
                more control over it. This can be accomplished using a Castor mapping file. For more information, refer
                to <a class="ulink" href="http://castor.org/xml-mapping.html" target="_top">Castor XML Mapping</a>.
            </p><p>
                The mapping can be set using the <span class="property">mappingLocation</span> resource property, indicated
                below with a classpath resource.
            </p><pre class="programlisting">
&lt;<span class="hl-tag">beans</span>&gt;
    &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"castorMarshaller"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.oxm.castor.CastorMarshaller"</span> &gt;
        &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"mappingLocation"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"classpath:mapping.xml"</span> /&gt;
    &lt;<span class="hl-tag">/bean</span>&gt;
&lt;<span class="hl-tag">/beans</span>&gt;
</pre></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="oxm-xmlbeans"></a>14.7&nbsp;XMLBeans</h2></div></div></div><p>
            XMLBeans is an XML binding tool that has full XML Schema support, and offers full XML Infoset
            fidelity. It takes a different approach to that of most other O/X mapping frameworks, in that
            all classes that are generated from an XML Schema are all derived from
            <code class="interfacename">XmlObject</code>, and contain XML binding information in them.
        </p><p>
            For more information on XMLBeans, refer to the <a class="ulink" href="http://xmlbeans.apache.org/" target="_top">
			   <em class="citetitle">XMLBeans web site </em></a>. The Spring-WS integration classes reside
            in the <span class="package">org.springframework.oxm.xmlbeans</span> package.
        </p><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="d0e26525"></a>14.7.1&nbsp;XmlBeansMarshaller</h3></div></div></div><p>
                The <code class="classname">XmlBeansMarshaller</code>
                implements both the <code class="interfacename">Marshaller</code>
                and <code class="interfacename">Unmarshaller</code>
                interfaces. It can be configured as follows:
            </p><pre class="programlisting">
&lt;<span class="hl-tag">beans</span>&gt;

    &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"xmlBeansMarshaller"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.oxm.xmlbeans.XmlBeansMarshaller"</span> /&gt;
    ...

&lt;<span class="hl-tag">/beans</span>&gt;</pre><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.gif"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>
                    Note that the <code class="classname">XmlBeansMarshaller</code>
                    can only marshal objects of type <code class="interfacename">XmlObject</code>,
                    and not every <code class="classname">java.lang.Object</code>.
                </p></td></tr></table></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="oxm-xmlbeans-xsd"></a>14.7.1.1&nbsp;XML Schema-based Configuration</h4></div></div></div><p>
	        		The <code class="literal">xmlbeans-marshaller</code> tag configures a <code class="classname">org.springframework.oxm.xmlbeans.XmlBeansMarshaller</code>.
	        		Here is an example:
	        	</p><pre class="programlisting">&lt;<span class="hl-tag">oxm:xmlbeans-marshaller</span> <span class="hl-attribute">id</span>=<span class="hl-value">"marshaller"</span>/&gt;</pre><p>
	        		Available attributes are:
                    </p><div class="informaltable"><table style="border-collapse: collapse;border-top: 1.0pt solid ; border-bottom: 1.0pt solid ; border-left: 1.0pt solid ; border-right: 1.0pt solid ; "><colgroup><col><col><col></colgroup><thead><tr><th style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; ">Attribute</th><th style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; ">Description</th><th style="border-bottom: 1.0pt solid ; ">Required</th></tr></thead><tbody><tr><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; "><code class="literal">id</code></td><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; ">the id of the marshaller</td><td style="border-bottom: 1.0pt solid ; ">no</td></tr><tr><td style="border-right: 1.0pt solid ; "><code class="literal">options</code></td><td style="border-right: 1.0pt solid ; ">the bean name of the XmlOptions that is to be used for this marshaller. Typically a
                                    <code class="classname">XmlOptionsFactoryBean</code> definition</td><td style="">no</td></tr></tbody></table></div><p>
	        	</p></div></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="oxm-jibx"></a>14.8&nbsp;JiBX</h2></div></div></div><p>
            The JiBX framework offers a solution similar to that which JDO provides for ORM: a binding definition defines the
            rules for how your Java objects are converted to or from XML. After preparing the binding and compiling the
            classes, a JiBX binding compiler enhances the class files, and adds code to handle converting instances of
            the classes from or to XML.
        </p><p>
            For more information on JiBX, refer to the <a class="ulink" href="http://jibx.sourceforge.net/" target="_top">
            <em class="citetitle">JiBX web site</em></a>. The Spring integration classes reside in the
            <span class="package">org.springframework.oxm.jibx</span> package.
        </p><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="d0e26617"></a>14.8.1&nbsp;JibxMarshaller</h3></div></div></div><p>
                The <code class="classname">JibxMarshaller</code> class implements both the 
                <code class="interfacename">Marshaller</code> and <code class="interfacename">Unmarshaller</code> interface.
                To operate, it requires the name of the class to marshal in, which you can set using the
                <span class="property">targetClass</span> property. Optionally, you can set the binding name using the
                <span class="property">bindingName</span> property. In the next sample, we bind the 
                <code class="classname">Flights</code> class:
            </p><pre class="programlisting">
&lt;<span class="hl-tag">beans</span>&gt;

    &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"jibxFlightsMarshaller"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.oxm.jibx.JibxMarshaller"</span>&gt;
        &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"targetClass"</span>&gt;org.springframework.oxm.jibx.Flights&lt;<span class="hl-tag">/property</span>&gt;
    &lt;<span class="hl-tag">/bean</span>&gt;

    ...
</pre><p>
                A <code class="classname">JibxMarshaller</code> is configured for a single class. If you want to marshal
                multiple classes, you have to configure multiple <code class="classname">JibxMarshaller</code>s with
                different <span class="property">targetClass</span> property values.
            </p><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="oxm-jibx-xsd"></a>14.8.1.1&nbsp;XML Schema-based Configuration</h4></div></div></div><p>
	        		The <code class="literal">jibx-marshaller</code> tag configures a <code class="classname">org.springframework.oxm.jibx.JibxMarshaller</code>.
	        		Here is an example:
	        	</p><pre class="programlisting">&lt;<span class="hl-tag">oxm:jibx-marshaller</span> <span class="hl-attribute">id</span>=<span class="hl-value">"marshaller"</span> <span class="hl-attribute">target-class</span>=<span class="hl-value">"org.springframework.ws.samples.airline.schema.Flight"</span>/&gt;</pre><p>
	        		Available attributes are:
                    </p><div class="informaltable"><table style="border-collapse: collapse;border-top: 1.0pt solid ; border-bottom: 1.0pt solid ; border-left: 1.0pt solid ; border-right: 1.0pt solid ; "><colgroup><col><col><col></colgroup><thead><tr><th style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; ">Attribute</th><th style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; ">Description</th><th style="border-bottom: 1.0pt solid ; ">Required</th></tr></thead><tbody><tr><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; "><code class="literal">id</code></td><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; ">the id of the marshaller</td><td style="border-bottom: 1.0pt solid ; ">no</td></tr><tr><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; "><code class="literal">target-class</code></td><td style="border-right: 1.0pt solid ; border-bottom: 1.0pt solid ; ">the target class for this marshaller</td><td style="border-bottom: 1.0pt solid ; ">yes</td></tr><tr><td style="border-right: 1.0pt solid ; "><code class="literal">bindingName</code></td><td style="border-right: 1.0pt solid ; ">the binding name used by this marshaller</td><td style="">no</td></tr></tbody></table></div><p>
	        	</p></div></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="oxm-xstream"></a>14.9&nbsp;XStream</h2></div></div></div><p>
            XStream is a simple library to serialize objects to XML and back again. It does not require any mapping, and
            generates clean XML.
        </p><p>
            For more information on XStream, refer to the <a class="ulink" href="http://xstream.codehaus.org/" target="_top">
			<em class="citetitle">XStream web site</em></a>. The Spring integration classes reside in the
            <span class="package">org.springframework.oxm.xstream</span> package.
        </p><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="d0e26722"></a>14.9.1&nbsp;XStreamMarshaller</h3></div></div></div><p>
                The <code class="classname">XStreamMarshaller</code> does not require any configuration, and can be configured
                in an application context directly. To further customize the XML, you can set an 
                <span class="emphasis"><em>alias map</em></span>, which consists of string aliases mapped to classes:
            </p><pre class="programlisting">
&lt;<span class="hl-tag">beans</span>&gt;

    &lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"xstreamMarshaller"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.oxm.xstream.XStreamMarshaller"</span>&gt;
        &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"aliases"</span>&gt;
            &lt;<span class="hl-tag">props</span>&gt;
                &lt;<span class="hl-tag">prop</span> <span class="hl-attribute">key</span>=<span class="hl-value">"Flight"</span>&gt;org.springframework.oxm.xstream.Flight&lt;<span class="hl-tag">/prop</span>&gt;
            &lt;<span class="hl-tag">/props</span>&gt;
        &lt;<span class="hl-tag">/property</span>&gt;
    &lt;<span class="hl-tag">/bean</span>&gt;
    ...

&lt;<span class="hl-tag">/beans</span>&gt;</pre><div class="warning" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Warning"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Warning]" src="images/warning.gif"></td><th align="left">Warning</th></tr><tr><td align="left" valign="top"><p>
                  By default, XStream allows for arbitrary classes to be unmarshalled, which can result in security
                  vulnerabilities.
                  As such, it is recommended to set the <span class="property">supportedClasses</span> property on the
                  <code class="classname">XStreamMarshaller</code>, like so:
                  </p><pre class="programlisting">&lt;<span class="hl-tag">bean</span> <span class="hl-attribute">id</span>=<span class="hl-value">"xstreamMarshaller"</span> <span class="hl-attribute">class</span>=<span class="hl-value">"org.springframework.oxm.xstream.XStreamMarshaller"</span>&gt;
    &lt;<span class="hl-tag">property</span> <span class="hl-attribute">name</span>=<span class="hl-value">"supportedClasses"</span> <span class="hl-attribute">value</span>=<span class="hl-value">"org.springframework.oxm.xstream.Flight"</span>/&gt;
    ...
&lt;<span class="hl-tag">/bean</span>&gt;</pre><p>
                  This will make sure that only the registered classes are eligible for unmarshalling.
                </p><p>
                  Additionally, you can register <a class="ulink" href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/oxm/xstream/XStreamMarshaller.html#setConverters(com.thoughtworks.xstream.converters.ConverterMatcher[])" target="_top">
                  custom converters</a> to make sure that only your supported classes can be unmarshalled.
                </p></td></tr></table></div><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.gif"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>
                  Note that XStream is an XML serialization library, not a data binding library. Therefore, it has
                  limited namespace support. As such, it is rather unsuitable for usage within Web services.
              </p></td></tr></table></div></div></div></div><!--Begin LoopFuse code--><script src="http://loopfuse.net/webrecorder/js/listen.js" type="text/javascript"></script><script type="text/javascript">
			_lf_cid = "LF_48be82fa";
			_lf_remora();
		</script><!--End LoopFuse code--><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="orm.html">Prev</a>&nbsp;</td><td width="20%" align="center"><a accesskey="u" href="spring-data-tier.html">Up</a></td><td width="40%" align="right">&nbsp;<a accesskey="n" href="spring-web.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">13.&nbsp;Object Relational Mapping (ORM) Data Access&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&nbsp;Part&nbsp;V.&nbsp;The Web</td></tr></table></div><!-- Begin 2nd Google code - a mod -->
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));</script>
<script type="text/javascript">
var OrgTracker = _gat._getTracker("UA-2728886-2"); OrgTracker._setDomainName("none");
OrgTracker._setAllowLinker(true);
OrgTracker._trackPageview();
</script>
 <!-- End 2nd Google code -->


<script type="text/javascript">

function detectLastFrame() {
   var thisF = this.window;
   var allF = parent.top.frames;
   return allF[allF.length - 1] == thisF; }

// Only inject the following code if this is a normal page or the last // frame of a frameset.
if (parent.top.frames.length == 0 || detectLastFrame()) {
    // Inject the SpringSource search widget too.
    document.write(unescape("%3Cscript src='http://search.springsource.org/widget/searchtool.js' type='text/javascript'%3E%3C/script%3E"));
    // Inject the Eloqua code
    document.write(unescape("%3Cscript src='/elqNow/elqCfg.js' type='text/javascript'%3E%3C/script%3E"));
    document.write(unescape("%3Cscript src='/elqNow/elqImg.js' type='text/javascript'%3E%3C/script%3E"));

}
</script>
</body>
</html>